package com.googlecode.linkmein.domain;

import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
import com.googlecode.linkmein.utils.Util;

public class Name {
    private String chineseName;
    private String englishName;
    public static final Name NULL = new NullName();

    public Name(String englishName, String chineseName) {
        this.chineseName = chineseName;
        this.englishName = englishName;
    }


    public Name merge(Name name) {
        String englishName = Util.isEmpty(name.englishName) ? this.englishName : name.englishName;
        String chineseName = Util.isEmpty(name.chineseName) ? this.chineseName : name.chineseName;
        return new Name(englishName, chineseName);
    }

    @Override
    public String toString() {
        return englishName + " - " + chineseName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Name name = (Name) o;
        return equalsIgnoreCase(chineseName, name.chineseName)
                || englishNameEquals(name);
    }

    private boolean englishNameEquals(Name name) {
        String thisEnglishName = englishName.replace(" ", "").toLowerCase();
        String otherEnglishName = name.englishName.replace(" ", "").toLowerCase();
        return equalsIgnoreCase(thisEnglishName, otherEnglishName);
    }

    @Override
    public int hashCode() {
        int result = chineseName != null ? chineseName.hashCode() : 0;
        result = 31 * result + (englishName != null ? englishName.hashCode() : 0);
        return result;
    }

    public String getEnglishName() {
        return englishName;
    }

    public String getChineseName() {
        return chineseName;
    }

    public String getShrinkedEnglishName() {
        return englishName.replace(" ", "").toLowerCase();
    }
}
